/**
 * Created by mhy on 2018/1/12.
 */

$(document).ready(function() {
    $("#grade").children("a").attr("onmouseover", "hoverStar(this)");
    $("#grade").children("a").attr("onclick", 'onclickStar(this)');
    var bookId = $("#bookId").text();

    $(".bs-example-modal-eb").find(".btn-primary").click(function () {
       var description = $("textarea").val();   //描述
       var grade = $("#grade").children("span").eq(0).text();
       var borrowId = $("#borrowId").text();
       var $form = $("<div></div>");
       var params;

        if(description == ""){
            createPNotify("提示信息","error","请输入描述");
            return;
        }else if(description.length > 100){
            createPNotify("提示信息","error","描述要小于100个字符");
            return;
        }else if(grade.length == 0){
            createPNotify("提示信息","error","请给书评个分数");
            return;
        }

        /*更新推荐*/
        /*基于用户的协同过滤*/
        $.ajax({
            type:'POST',//请求类型
            url:'updateUserRecommendation',//请求地址
            contentType: 'application/json;',//发送信息至服务器时内容编码类型
            data:JSON.stringify({
                "bookId":bookId,
            }),
            dataType:'JSON',/*预期服务器返回的数据类型*/
            cache:true,
        });

        /*更新推荐*/
        /*基于物品的协同过滤*/
        $.ajax({
            type:'POST',//请求类型
            url:'updateBookRecommendation',//请求地址
            contentType: 'application/json;',//发送信息至服务器时内容编码类型
            data:JSON.stringify({
            }),
            dataType:'JSON',/*预期服务器返回的数据类型*/
            cache:true,
        });

        params = $("<div class='postParam'></div>").clone();
        params.attr("id","grade");
        params.text(grade);
        $form.append(params);

        params = $("<div class='postParam'></div>").clone();
        params.attr("id","description");
        params.text(description);
        $form.append(params);

        params = $("<div class='postParam'></div>").clone();
        params.attr("id","borrowId");
        params.text(borrowId);
        $form.append(params);

        postForm("bookEvalue",$form);
    });

    //返回
    $("button.btn-info").click(function () {
        goLastPage();
    })
});

function hoverStar(e){
    var $this = $(e);
    var preStar = $this.prevAll("a");
    var nextStar = $this.nextAll("a");

    $this.children("i").removeClass("fa-star-o");
    $this.children("i").addClass("fa-star");
    preStar.children("i").removeClass("fa-star-o");
    preStar.children("i").addClass("fa-star");
    nextStar.children("i").removeClass("fa-star");
    nextStar.children("i").addClass("fa-star-o");

    $("#grade").children("a").attr("onmouseout", 'outStar(this)');
}

function outStar(e) {
    var $this = $(e);
    var preStar = $this.prevAll("a");

    $this.children("i").removeClass("fa-star");
    $this.children("i").addClass("fa-star-o");
    preStar.children("i").removeClass("fa-star");
    preStar.children("i").addClass("fa-star-o");
}

function onclickStar(e) {
    var $this = $(e);
    var preStar = $this.prevAll("a");
    var desSpan = $("<span></span>");
    var $span = $this.siblings("span");

    for(var i = 1;i < $span.length;++i){
        $span.eq(i).remove();
    }

    $this.children("i").removeClass("fa-star-o");
    $this.children("i").addClass("fa-star");
    preStar.children("i").removeClass("fa-star-o");
    preStar.children("i").addClass("fa-star");

    $span.eq(0).text(toDecimal2(preStar.length + 1));

    if(preStar.length == 0){
        desSpan.text("极差");
    } else if(preStar.length == 1){
        desSpan.text("较差");
    } else if(preStar.length == 2){
        desSpan.text("一般");
    } else if(preStar.length == 3){
        desSpan.text("不错");
    } else if(preStar.length == 4){
        desSpan.text("优秀");
    }
    $this.siblings("span").after(desSpan);
    $("#grade").children("a").removeAttr("onmouseout");
}


function toDecimal2(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x*100)/100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 1) {
        s += '0';
    }
    return s;
}